package com.whj.ems.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.whj.ems.entity.Company;
import com.whj.ems.entity.User;
import com.whj.ems.service.CompanyService;
import com.whj.ems.util.EncryptAndDecodeUtil;

@Controller
public class CompanyController {
	Map<String , Object> maps = new HashMap<String, Object>();
	HttpEntity<Object> httpEntity=null;
	String total="";
	@Autowired
	private CompanyService companyService;
	@RequestMapping(value="/getCompanyData.do")
	public HttpEntity<Object> getCompanyData(@RequestParam("rows")String rows,@RequestParam("page")String page
			,@RequestParam("searchType")String searchType,@RequestParam("urlPamer")String urlPamer){
		List<Company> companys=new ArrayList<Company>();
		int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
	    //每页显示条数  
	    int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
	    //每页的开始记录  第一页为1  第二页为number +1   
	    int start = (intPage-1)*number;  
		maps.clear();
		try {
			 companys = this.companyService.getByfenye(start, number,searchType,urlPamer);
			 total=String.valueOf(companys.size());
			maps.put("total",this.companyService.getCountCompany(Company.class));
			maps.put("rows",companys);
			} catch (Exception e1) {
					maps.put("failure","true");
					maps.put("message","查询数据时发生异常请检查后重试！");
					e1.printStackTrace();
			}
			httpEntity=new HttpEntity<Object>(maps);
			return httpEntity;
	}
	@RequestMapping(value="/addCompany.do")
	public HttpEntity<Object> addCompany(Company company){
		HttpEntity<Object> httpEntity =null; 
		maps.clear();
		try {
			    this.companyService.save(company);
				maps.put("success", true);
				maps.put("message", "保存成功！");	
			} catch (Exception e1) {
				e1.printStackTrace();
				maps.put("failure", true);
				maps.put("message", "保存失败！");	
			}
		httpEntity=new HttpEntity<Object>(maps);
		return httpEntity;
	}
	@RequestMapping(value="/getCompanyKey.do")
	public HttpEntity<Object> getCompanyKey(@RequestParam("gtelephone") String gtelephone){
		HttpEntity<Object> httpEntity =null; 
		String initUsername="";
		String initPassword="";
		maps.clear();
		try {
			EncryptAndDecodeUtil encryptAndDecodeUtil=new EncryptAndDecodeUtil();
			encryptAndDecodeUtil.setKey("8238982");
			System.out.println(gtelephone);
			encryptAndDecodeUtil.setEncString(gtelephone);
			initUsername = encryptAndDecodeUtil.getStrMi();
			encryptAndDecodeUtil.setEncString("123456");
			initPassword = encryptAndDecodeUtil.getStrMi();
			maps.put("success", true);
			maps.put("username", initUsername);
			maps.put("password", initPassword);
				httpEntity = new HttpEntity<Object>(maps);
			} catch (Exception e1) {
				maps.put("success", false);
				httpEntity = new HttpEntity<Object>(maps);
				e1.printStackTrace();
			}
		return httpEntity;
	}
	@RequestMapping(value="/deleteCompany.do")
	public  HttpEntity<Object>  deleteCompany(@RequestParam("gids") String gids){
		HttpEntity<Object> httpEntity =null; 
		maps.clear();
		try {
				List<Company> companies = new ArrayList<Company>();
				String gidsArr[]=gids.split(",");
				if(gidsArr.length>1){
					for(int i=0;i<gidsArr.length;i++){
						Company company = new  Company();
						company.setGid(Integer.parseInt(gidsArr[i]));
						companies.add(company);
					}
					this.companyService.deleteByIds(companies);
				}else{
					this.companyService.deleteById(Integer.parseInt(gidsArr[0]));
				}
				maps.put("isSuccess", true);
			} catch (Exception e1) {
				e1.printStackTrace();
				maps.put("isSuccess", false);
			}
		httpEntity = new HttpEntity<Object>(maps);
		return httpEntity;
	}
	@RequestMapping(value="/updateCompany.do")
	public  HttpEntity<Object>   updateTitle(Company company){
		HttpEntity<Object> httpEntity =null; 
		maps.clear();
		try {
				this.companyService.updateById(company);
				maps.put("success", true);
				maps.put("message", "更新成功！");
			} catch (Exception e1) {
				e1.printStackTrace();
				maps.put("failure", true);
				maps.put("message", "更新失败！");
			}
		httpEntity = new HttpEntity<Object>(maps);
		return httpEntity;
	}
}
